
%% Golden Search Update

% Nothing is particularly hard if you divide it into small jobs. – Henry Ford

web('http://www.rff.com/flowchart_shapes.htm')

% Our array convention:
% X1------X3------X2
% [X1 X3 X2]
% [F1 F3 F2]

%Make starting values for F and X
F
X
f %function pointer


%The Golden Ratio b/a
Phi=(1+sqrt(5))/2;
%a/(a+b)
R=1/(1+Phi);

% Check if interval is right or left
% Left:
% |--a--|-----b-----|
% Right:
% |-----b-----|--a--|
if (X(2)-X(1))>( X(3)-X(2))
    IntervalSide='right'
else
    IntervalSide='left'
end

switch IntervalSide
    case 'right' % |-----b-----|--a--|
        % Calculate new X4 value
        X4=X(2)-R*(X(2)-X(1));
        % Calculate new F4 value
        F4=f(X4);
        
        if F(2)<F4 % Check if F3<F4
            % Return New Interval
            X=[X4,X(2),X(3)];
            F=[F4,F(2),F(3)];
        else
            % Return New Interval
            X=[X(1),X4,X(2)];
            F=[F(1),F4,F(2)];
        end
        
    case 'left' % |--a--|-----b-----|
        % Calculate new X4 value
        X4=X(2)+R*(X(3)-X(2));
        % Calculate new F4 value
        F4=f(X4);
        
        % Check if F3<F4
        if F(2)<F4
            % Return New Interval
            X=[X(1),X(2),X4]
            F=[F(1),F(2),F4]
        else
            % Return New Interval
            X=[X(2),X4,X(3)]
            F=[F(2),F4,F(3)]
        end
        
        
end

















